//
//  registerTableViewCell.m
//  jinling
//
//  Created by mac on 2018/9/30.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "registerTableViewCell.h"

@implementation registerTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)cratView{
    UILabel *label=[UILabel new];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label.text=@"推荐人ID";
    label.font=[UIFont systemFontOfSize:14];
    
    
    _tuJianText=[UITextField new];
    [self addSubview:_tuJianText];
    [_tuJianText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right).mas_equalTo(30);
        make.centerY.mas_equalTo(label);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(-30);
    }];
    _tuJianText.text=@"10001";
    _tuJianText.font=[UIFont systemFontOfSize:14];
    _tuJianText.returnKeyType=UIReturnKeyDone;
    _tuJianText.delegate=self;
    
    UIView *xianView=[UIView new];
    [self addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UILabel *label3=[UILabel new];
    [self addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label3.text=@"手机号码";
    label3.font=[UIFont systemFontOfSize:14];
    
    _phoneText=[UITextField new];
    [self addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self->_tuJianText);
        make.centerY.mas_equalTo(label3);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(20);
    }];
    _phoneText.placeholder=@"请输入手机号码";
    _phoneText.font=[UIFont systemFontOfSize:14];
    _phoneText.returnKeyType=UIReturnKeyDone;
    _phoneText.delegate=self;
    
    
    
    UIView *xianView2=[UIView new];
    [self addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label3.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView2.backgroundColor=_LightGrey;
    
    UILabel *label4=[UILabel new];
    [self addSubview:label4];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView2.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label4.text=@"所在地区";
    label4.font=[UIFont systemFontOfSize:14];
    
    
    _diquLabel=[UILabel new];
    [self addSubview:_diquLabel];
    [_diquLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self->_tuJianText);
        make.centerY.mas_equalTo(label4);
        make.height.mas_equalTo(30);
    }];
    _diquLabel.text=@"请选择当前地区";
    _diquLabel.textColor=ColorWithRGB(149, 149, 149);
    _diquLabel.font=[UIFont systemFontOfSize:14];
    _diquLabel.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture14 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getDiQu)];
    [_diquLabel addGestureRecognizer:tapGesture14];
    
    UIImageView *jianTou=[UIImageView new];
    [self addSubview:jianTou];
    [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self->_diquLabel);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(15);
    }];
    jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
    
    
    UIView *xianView3=[UIView new];
    [self addSubview:xianView3];
    [xianView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label4.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView3.backgroundColor=_LightGrey;
    
    UILabel *label5=[UILabel new];
    [self addSubview:label5];
    [label5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView3.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label5.text=@"登录密码";
    label5.font=[UIFont systemFontOfSize:14];
    
    
    _pwdText=[UITextField new];
    [self addSubview:_pwdText];
    [_pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self->_tuJianText);
        make.centerY.mas_equalTo(label5);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(20);
    }];
    _pwdText.placeholder=@"请输入登录密码";
    _pwdText.font=[UIFont systemFontOfSize:14];
    _pwdText.returnKeyType=UIReturnKeyDone;
    _pwdText.delegate=self;
    _pwdText.secureTextEntry=YES;//密文

    
    
    UIView *xianView4=[UIView new];
    [self addSubview:xianView4];
    [xianView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label5.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView4.backgroundColor=_LightGrey;
    
    UILabel *label6=[UILabel new];
    [self addSubview:label6];
    [label6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView4.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label6.text=@"确认密码";
    label6.font=[UIFont systemFontOfSize:14];
    
    
    _pwdText2=[UITextField new];
    [self addSubview:_pwdText2];
    [_pwdText2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self->_tuJianText);
        make.centerY.mas_equalTo(label6);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(20);
    }];
    _pwdText2.placeholder=@"请再次输入登录密码";
    _pwdText2.font=[UIFont systemFontOfSize:14];
    _pwdText2.returnKeyType=UIReturnKeyDone;
    _pwdText2.delegate=self;
    _pwdText2.secureTextEntry=YES;//密文
    
    
    UIView *xianView5=[UIView new];
    [self addSubview:xianView5];
    [xianView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label6.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView5.backgroundColor=_LightGrey;
    
    UILabel *label7=[UILabel new];
    [self addSubview:label7];
    [label7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView5.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label7.text=@"短信验证码";
    label7.font=[UIFont systemFontOfSize:14];
    

    
    _codeText=[UITextField new];
    [self addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label7.mas_right).mas_equalTo(30);
        make.centerY.mas_equalTo(label7);
        make.right.mas_equalTo(-100);
        make.height.mas_equalTo(20);
    }];
    _codeText.delegate=self;
    _codeText.placeholder=@"请输入验证码";
    _codeText.font=[UIFont systemFontOfSize:14];
    _codeText.returnKeyType=UIReturnKeyDone;
//    _codeText.keyboardType=UIKeyboardTypePhonePad;
    
    
    _codeBtn=[UIButton new];
    [self addSubview:_codeBtn];
    [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.centerY.mas_equalTo(self->_codeText);
    }];
    _codeBtn.layer.masksToBounds=YES;
    _codeBtn.layer.cornerRadius=5;
    _codeBtn.layer.borderWidth=1;
    _codeBtn.layer.borderColor=_backgroundColor.CGColor;
    [_codeBtn setTitle:@"发送验证码" forState:0];
    [_codeBtn setTitleColor:_backgroundColor forState:0];
    _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
    [_codeBtn  addTarget:self action:@selector(huoQuYanZhen:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *xianview6=[UIView new];
    [self addSubview:xianview6];
    [xianview6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label7.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianview6.backgroundColor=_LightGrey;
    
    
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(xianview6.mas_bottom).mas_equalTo(25);
        make.height.mas_equalTo(40);
    }];
    btn.layer.cornerRadius=5;
    btn.backgroundColor=_backgroundColor;
    [btn setTitle:@"注册" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:17];
    [btn addTarget:self action:@selector(zhuCe:) forControlEvents:UIControlEventTouchUpInside];

    
    
}

-(void)zhuCe:(UIBarButtonItem *)btn{
    
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSString *tuiJina=_tuJianText.text;

    if (_selectArr==nil) {
        [gongju svpInfo:@"请选择地区"];
        return;
    }
    NSString *pwdStr=_pwdText.text;
    if (![self checkPassWord:pwdStr]) {
        [gongju svpInfo:@"密码必须是有6到20位的数字和字母组成(必须包含字母和数字)"];
        return;
    }
    NSString *pwdStr2=_pwdText2.text;
    if (![pwdStr isEqualToString:pwdStr2]) {
        [gongju svpInfo:@"两次密码不一致,请重新输入"];
        return;
    }

    NSString *codeStr=_codeText.text;
    if ([gongju charIsNil:codeStr]) {
        [gongju svpInfo:@"验证码不能为空"];
        return;
    }

    NSMutableDictionary *typeDic=[NSMutableDictionary dictionaryWithCapacity:0];

    [typeDic setObject:mobile forKey:@"user_phone"];
    [typeDic setObject:tuiJina forKey:@"referee_id"];
    [typeDic setObject:pwdStr forKey:@"password"];

    NSDictionary *dic1=_selectArr[0];
    [typeDic setObject:[dic1 objectForKey:@"value"] forKey:@"province"];


    NSDictionary *dic2=_selectArr[1];
    [typeDic setObject:[dic2 objectForKey:@"value"] forKey:@"city"];


    NSDictionary *dic3=_selectArr[2];
    [typeDic setObject:[dic3 objectForKey:@"value"] forKey:@"area"];

    [typeDic setObject:@"18323885152" forKey:@"referee_phone"];

    [gongju AFNGETDataUrl:[NSString stringWithFormat:@"sms/get_code?phone=%@",mobile] andDic:nil andTableView:nil andView:nil andSuccessBlock:^(id responseObject) {

        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        if (![str isEqualToString:codeStr]) {
            [gongju svpInfo:@"验证码已过期,请重新获取"];
        }
        else{
            [self yanZheng:typeDic];
        }
    } anderrorBlock:nil];
}

-(void)yanZheng:(NSDictionary *)typeDic{
    

    
        [gongju AFNPOSTDataUrl:@"user/user_regist" andDic:typeDic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
    
            [gongju svpInfo:@"注册成功"];
            [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile];

            [self tianJia:responseObject];
            
      
        } anderrorBlock:nil];
    
}

-(void)tianJia:(NSDictionary *)typeDic{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:typeDic options:0 error:0];
    NSString *dataStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    

    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:dataStr,@"user", nil];
    [gongju AFNPOSTDataUrl:@"page/app_add_user.html" andDic:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        
        [self.viewController.navigationController popToRootViewControllerAnimated:YES];
        
    } anderrorBlock:nil];
}



-(IBAction)huoQuYanZhen:(UIButton *)sender{
    
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"phone",@"1",@"code", nil];
    [gongju AFNPOSTDataUrl:@"sms/send_sms" andDic:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        
        self->_codeBtn.enabled=NO;
        self->_timeTick=60;
        self->_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        
    } anderrorBlock:nil];
    
}

-(void)getDiQu{
    
    [_tuJianText resignFirstResponder];
    [_phoneText resignFirstResponder];
    [_pwdText resignFirstResponder];
    [_pwdText2 resignFirstResponder];
    [_codeText resignFirstResponder];
    

    [gongju AFNGETDataUrl:@"setting/get_city" andDic:nil andTableView:nil andView:self andSuccessBlock:^(id responseObject) {

        self->_diZhiView =[diZhiView new];
        [self.viewController.view addSubview:self->_diZhiView];
        [self->_diZhiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(0);
        }];
        self-> _diZhiView.dataArr=[NSMutableArray arrayWithCapacity:0];
        [self-> _diZhiView.dataArr addObjectsFromArray:[responseObject objectForKey:@"data"]];
        self->_diZhiView.customSelectedBlock = ^(NSArray *selectArr) {
            self->_selectArr=selectArr;
            NSString *dizhiText;
            for (int i=0; i<selectArr.count; i++) {
                
                NSDictionary *dic=selectArr[i];
                if (i==0) {
                    dizhiText=[NSString stringWithFormat:@"%@",[dic objectForKey:@"text"]];
                }
                else{
                    dizhiText=[NSString stringWithFormat:@"%@ %@",dizhiText,[dic objectForKey:@"text"]];
                }
                
            }

            self->_diquLabel.text=dizhiText;

        };
        [self->_diZhiView creatView];
        
    } anderrorBlock:nil];

}

-(void)timeFireMethod
{
    if (_timeTick==0) {
        _codeBtn.enabled=YES;
        _codeBtn.layer.cornerRadius=5;
        _codeBtn.layer.borderWidth=1;
        _codeBtn.layer.borderColor=_backgroundColor.CGColor;
        _codeBtn.layer.borderColor=_backgroundColor.CGColor;
        [_codeBtn setTitle:@"发送验证码" forState:0];
        [_codeBtn setTitleColor:_backgroundColor forState:0];
        _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
        _codeBtn.backgroundColor=[UIColor clearColor];
        [_timer invalidate];
        return;
    }
    _timeTick--;
    [_codeBtn setTitle:[NSString stringWithFormat:@"%dS重发",_timeTick] forState:0];
    _codeBtn.backgroundColor=_DarkGrey;
    _codeBtn.layer.cornerRadius=5;
    _codeBtn.layer.borderWidth=0;
    _codeBtn.layer.borderColor=[UIColor clearColor].CGColor;
    _codeBtn.layer.borderColor=[UIColor clearColor].CGColor;
    [_codeBtn setTitleColor:_backgroundColor forState:0];
    _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    textField.backgroundColor=[UIColor clearColor];
    return YES;
}


-(void)popVc{
    
    UINavigationController *navVC = self.viewController.navigationController;
    
    NSMutableArray *viewControllers2 = [[NSMutableArray alloc] init];
    
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    
    for (UIViewController *vc in [navVC viewControllers]) {
        
        [viewControllers2 addObject:vc];

        
    }
    
    
    for (int i=0; i<viewControllers2.count-2; i++) {
        UIViewController *vc=viewControllers2[i];
        [viewControllers addObject:vc];
    }
    
    [navVC setViewControllers:viewControllers animated:YES];
    
}
-(BOOL)checkPassWord:(NSString *)pwdStr
{
    //6-20位数字和字母组成
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:pwdStr]) {
        return YES ;
    }else
        return NO;
}



@end
